package com.zj.leetcode.medium.greed;

/**
 * @program: algorithm
 * @description: 强密码检验器
 * 420 strong-password-checker
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class StrongPasswordChecker {
    public static void main(String[] args) {
//        String password = "aaa111";
//        String password = "aaaaaaabbb";
//        String password = "ABABABABABABABABABAB1";
        String password = "aaaabaaaaaa123456789F";
        Solution solution = new StrongPasswordChecker().new Solution();
        System.out.println(solution.strongPasswordChecker(password));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int strongPasswordChecker(String password) {

            if (password.length() <= 1) {
                return 6 - password.length();
            }

            //长度小于6
            int one = 0;
            //大写，小写，数字
            boolean big = false;
            boolean small = false;
            boolean num = false;
            int two = 0;


            //不能出现连续相同的字符
            int iden = 1;
            char last = password.charAt(0);
            int three = 0;

            //len 大于20
            int four = 0;


            for (int i = 0; i < password.length(); i++) {
                char temp = password.charAt(i);


                //大写，小写，数字
                if (Character.isLowerCase(temp)) {
                    small = true;
                }
                if (Character.isUpperCase(temp)) {
                    big = true;
                }
                if (Character.isDigit(temp)) {
                    num = true;
                }

                //不能出现连续相同的字符
                if (i != 0 && last == temp) {
                    iden++;
                } else {
                    three = three + iden / 3;
                    iden = 1;
                }

                last = temp;

            }

            three = three + iden / 3;


            if (!big) {
                two++;
            }
            if (!small) {
                two++;
            }
            if (!num) {
                two++;
            }


            if (password.length() < 6) {
                one = 6 - password.length();
            }
            if (password.length() > 20) {
                four = password.length() - 20;
            }

            return Math.max(Math.max(one, two), three);

        }


        public int strongPasswordChecker1(String password) {
            int result= 0;
            int small = 1, big = 1 ,digit = 1;
            char[] arr = password.toCharArray();
            int[] a = new int[arr.length];
            for(int i = 0; i < a.length;){
                if(Character.isLowerCase(arr[i]))   small = 0;
                if(Character.isUpperCase(arr[i]))   big = 0;
                if(Character.isDigit(arr[i]))   digit = 0;
                int j = i;
                while(i < arr.length && arr[i] == arr[j]) i++;
                a[j] = i - j;
            }
            int lack = small + big + digit;
            if(a.length < 6){
                result += lack + Math.max( 0 , 6 -(a.length + lack));
            } else {
                int over = Math.max(a.length - 20 ,0);
                int left = 0;
                result += over;
                for(int k = 1; k < 3 ; k++){
                    for(int i = 0 ; i < a.length && over > 0 ;i++){
                        if(a[i] < 3 || a[i]%3 != (k-1))
                            continue;
                        a[i] -= Math.min(over , k);
                        over -= k;
                    }
                }
                for(int i = 0; i < a.length ; i++){
                    if(a[i] >= 3 && over > 0){
                        int need = a[i] - 2;
                        a[i] -= over;
                        over -= need;
                    }
                    if(a[i] >= 3) left += a[i] / 3;
                }
                result += Math.max(lack , left);
            }
            return result;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
